package com.open.capacity.reimburse.controller;

import com.open.capacity.annotation.log.LogAnnotation;
import com.open.capacity.commons.PageResult;
import com.open.capacity.commons.Result;
import com.open.capacity.reimburse.entity.StdCar;
import com.open.capacity.reimburse.service.StdCarService;
import com.open.capacity.reimburse.service.StdCarService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * 差旅住宿标准
 *
 * @author
 * @email
 * @date 2021-01-13 11:04:27
 */
@Slf4j
@RestController
@RequestMapping("stdcars")
@Api(tags = "公车补贴标准")
public class StdCarController {

    @Autowired
    private StdCarService stdCarService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('std_car:stdcar:list')")
    public PageResult list(@RequestParam Map<String, Object> params) {
        PageResult pageResult = stdCarService.pageAll(params);
        return pageResult;
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('std_car:stdcar:list')")
    public Result findById(String id) {
        return Result.succeed(stdCarService.findById(id));
    }

    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate")
    @PreAuthorize("hasAnyAuthority('std_car:stdcar:save')")
    @LogAnnotation(module = "公务用车标准", name = "保存")
    public Result save(@RequestBody @Valid StdCar stdCar, String keyValue) {
        if (StringUtils.isEmpty(keyValue)) {
            try {
                stdCarService.save(stdCar);
            } catch (Exception e) {
                return Result.failed(e.getMessage());
            }
        } else {
            stdCar.setId(keyValue);
            stdCarService.update(stdCar);
        }
        return Result.succeed("保存成功");
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAnyAuthority('std_car:stdcar:delete')")
    public Result delete(@PathVariable String id) {
        stdCarService.delete(id);
        return Result.succeed("删除成功");
    }
}
